<?php

/**
 * 公共控制器，被继承以起到相应作用
 */
class CommonAction extends Action {

    /**
     * 自动验证用户是否有登录，如果未登录跳回到登录页面
     * @return [type]
     */
//	public function _initialize(){
//		if(!isset($_SESSION['mid']) || !isset($_SESSION['uname'])){
//			redirect(U('Login/index'));
//		}
//	}
    /**
     * 上传方法
     * @return type 
     */
    public function upload() {
        import('ORG.Net.UploadFile');
        $upload = new UploadFile(); // 实例化上传类 
        $upload->maxSize = 3000000; // 讴置附件上传大小 
        $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg'); // 讴置附件上传类型 
        $upload->uploadReplace = true; //覆盖同名文件
        $upload->saveRule = 'uniqid';
        $upload->savePath = './Uploads/Breed/';
        $upload->autoSub = true; //使用子目录保存
        $upload->subType = 'date'; //使用日期子目录
        $upload->dateFormat = 'Y-m'; //按月份保存
        if ($upload->upload()) {
            $info = $upload->getUploadFileInfo();
            echo json_encode(array('status' => 1, 'path' => $info[0]['savename']));
        } else {
            echo json_encode(array('status' => 0, 'msg' => $upload->getErrorMsg()));
        }
    }

    /**
     * 多图上传
     * @return type 
     */
    public function uploads() {
       import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize = 2000000 ;	//最大上传大小
		$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');//充许上传文件的类型
		$upload->saveRule = 'uniqid';
		$upload->savePath = './Uploads/detail/';
		$upload->autoSub = true;//使用子目录保存
		$upload->subType = 'date';//使用日期子目录
		$upload->dateFormat = 'Y-m';//按月份保存
		$upload->thumb = true;	//生成缩略图
		$upload->thumbMaxWidth = '300,100'; //缩略图宽度
		$upload->thumbMaxHeight = '200,80';	//缩略图高度
		$upload->thumbPrefix = 'medium_,mini_';	//缩略图前缀名
		$upload->uploadReplace = false;	//覆盖原图
		$upload->thumbRemoveOrigin = false;
		$upload->thumbPath = './Uploads/detail/' . date('Y-m') . '/';
		if ($upload->upload()) {
			$info = $upload->getUploadFileInfo();
			$pic =  explode('/', $info[0]['savename']);
			$path = array(
				'max' => $info[0]['savename'],
				'medium' => $pic[0] . '/medium_' . $pic[1],
				'mini' => $pic[0] . '/mini_' . $pic[1]
				);
			echo json_encode(array('status' => 1, 'path' => $path));
		} else {
			echo json_encode(array('status' => 0, 'msg' => $upload->getErrorMsg()));
		}
    }

    public function up() {
        import('ORG.Net.UploadFile');
        $upload = new UploadFile(); // 实例化上传类
        $upload->maxSize = 2000000; //最大上传大小
        $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg'); //充许上传文件的类型
        $upload->saveRule = 'uniqid';
        $upload->savePath = './Uploads/List/';
        $upload->autoSub = true; //使用子目录保存
        $upload->subType = 'date'; //使用日期子目录
        $upload->dateFormat = 'Y-m'; //按月份保存
        $upload->thumb = true; //生成缩略图
        $upload->thumbMaxWidth = 200; //缩略图宽度
        $upload->thumbMaxHeight = 130; //缩略图高度
        $upload->thumbPrefix = ''; //缩略图前缀名
        $upload->uploadReplace = true; //覆盖原图
        $upload->thumbRemoveOrigin = false;
        $upload->thumbPath = './Uploads/List/' . date('Y-m') . '/';
        if ($upload->upload()) {
            $info = $upload->getUploadFileInfo();
            echo json_encode(array('status' => 1, 'path' => $info[0]['savename']));
        } else {
            echo json_encode(array('status' => 0, 'msg' => $upload->getErrorMsg()));
        }
    }

}

?>